package handler

import (
	"context"
	"usercenter/internal/cache"
	"usercenter/internal/model"
	"usercenter/internal/service"
)

type UserService struct {
	service.UnimplementedUserServiceServer
}

func (u *UserService) UUIDExec(ctx context.Context, req *service.ExecUUIDRequest) (*service.ExecUUIDResponse, error) {
	// 提取缓存操作
	uuid := req.Uuid
	res, err := cache.RedisDB.HGetAll("user:link:" + uuid).Result()
	if err != nil {
		return &service.ExecUUIDResponse{Code: 1202}, nil
	}

	// 执行操作
	switch res["operate"] {
	case "register":
		mobile := res["mobile"]
		err := model.InsertUser(&model.User{
			Nickname: res["nickname"],
			Email:    res["email"],
			Password: res["pwd"],
			Mobile:   &mobile,
		})
		if err != nil {
			return &service.ExecUUIDResponse{Code: 1203}, nil
		}
		break
	default:
		return &service.ExecUUIDResponse{Code: 1400}, nil
	}

	return &service.ExecUUIDResponse{Code: 1200}, nil
}
